package com.example.restful01.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.restful01.model.BorrowRecords;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

@Mapper
public interface BorrowRecordsMapper extends BaseMapper<BorrowRecords> {
    @Delete("DELETE FROM borrow_records WHERE reader_id = #{readerId} AND book_id = #{bookId} ")
    int deleteBorrow(
            @Param("readerId") Integer readerId,
            @Param("bookId") Integer bookId
    );
    @Insert("INSERT INTO `borrow_records` (\n" +
            "    `record_id`, \n" +
            "    `reader_id`, \n" +
            "    `book_id`, \n" +
            "    `book_name`, \n" +
            "    `borrow_date`, \n" +
            "    `return_date`, \n" +
            "    `real_date`, \n" +
            "    `reader_name`\n" +
            ")\n" +
            "values( \n" +
            "    NULL, \n" +
            "    #{readerId}, \n" +
            "    #{bookId},\n" +
            "    #{bookName},\n" +
            "    NULL,  \n" +
            "    NULL,  \n" +
            "    NULL,  \n" +
            "    #{readerName});\n")
    int addBorrow(BorrowRecords borrowRecords);
@Select("select * from borrow_records where reader_id = #{readerId}")
    List<BorrowRecords> initBorrows(Integer readerId);
@Update("UPDATE borrow_records SET status = '已还' WHERE record_id = #{recordId} ")
    int returnBook(Integer recordId);
@Update("UPDATE borrow_records SET borrow_date = NOW(),status = '续借' WHERE record_id = #{recordId}")
    int continueBorrow(Integer recordId);
@Select("SELECT * FROM borrow_records WHERE reader_name LIKE CONCAT('%', #{info}, '%') OR book_name LIKE CONCAT('%', #{info}, '%')")
    List<BorrowRecords> searchBorrow(String info);
}
